package com.kexio.common.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 统一响应结果类
 * 
 * 用于封装API接口的统一响应格式
 * 
 * @param <T> 数据类型
 * @author Kexio Team
 * @since 1.0.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /** 成功状态码 */
    public static final int SUCCESS_CODE = 200;
    
    /** 失败状态码 */
    public static final int ERROR_CODE = 500;
    
    /** 参数错误状态码 */
    public static final int BAD_REQUEST_CODE = 400;
    
    /** 未授权状态码 */
    public static final int UNAUTHORIZED_CODE = 401;
    
    /** 禁止访问状态码 */
    public static final int FORBIDDEN_CODE = 403;
    
    /** 资源不存在状态码 */
    public static final int NOT_FOUND_CODE = 404;
    
    // ==================== 字段定义 ====================
    
    /** 响应状态码 */
    private int code;
    
    /** 响应消息 */
    private String message;
    
    /** 响应数据 */
    private T data;
    
    /** 响应时间戳 */
    private LocalDateTime timestamp;
    
    /** 请求跟踪ID */
    private String traceId;
    
    // ==================== 构造函数 ====================
    
    public Result() {
        this.timestamp = LocalDateTime.now();
    }
    
    public Result(int code, String message) {
        this();
        this.code = code;
        this.message = message;
    }
    
    public Result(int code, String message, T data) {
        this(code, message);
        this.data = data;
    }
    
    // ==================== 静态工厂方法 ====================
    
    /**
     * 成功响应（无数据）
     */
    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, "操作成功");
    }
    
    /**
     * 成功响应（有数据）
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, "操作成功", data);
    }
    
    /**
     * 成功响应（自定义消息）
     */
    public static <T> Result<T> success(String message) {
        return new Result<>(SUCCESS_CODE, message);
    }
    
    /**
     * 成功响应（自定义消息和数据）
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(SUCCESS_CODE, message, data);
    }
    
    /**
     * 失败响应（默认消息）
     */
    public static <T> Result<T> error() {
        return new Result<>(ERROR_CODE, "操作失败");
    }
    
    /**
     * 失败响应（自定义消息）
     */
    public static <T> Result<T> error(String message) {
        return new Result<>(ERROR_CODE, message);
    }
    
    /**
     * 失败响应（自定义状态码和消息）
     */
    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message);
    }
    
    /**
     * 参数错误响应
     */
    public static <T> Result<T> badRequest(String message) {
        return new Result<>(BAD_REQUEST_CODE, message);
    }
    
    /**
     * 未授权响应
     */
    public static <T> Result<T> unauthorized(String message) {
        return new Result<>(UNAUTHORIZED_CODE, message);
    }
    
    /**
     * 禁止访问响应
     */
    public static <T> Result<T> forbidden(String message) {
        return new Result<>(FORBIDDEN_CODE, message);
    }
    
    /**
     * 资源不存在响应
     */
    public static <T> Result<T> notFound(String message) {
        return new Result<>(NOT_FOUND_CODE, message);
    }
    
    // ==================== 判断方法 ====================
    
    /**
     * 判断是否成功
     */
    @JsonProperty("success")
    public boolean isSuccess() {
        return this.code == SUCCESS_CODE;
    }
    
    /**
     * 判断是否失败
     */
    public boolean isError() {
        return !isSuccess();
    }
    
    // ==================== Getter 和 Setter ====================
    
    public int getCode() {
        return code;
    }
    
    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }
    
    public String getMessage() {
        return message;
    }
    
    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }
    
    public T getData() {
        return data;
    }
    
    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
    
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    public Result<T> setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
        return this;
    }
    
    public String getTraceId() {
        return traceId;
    }
    
    public Result<T> setTraceId(String traceId) {
        this.traceId = traceId;
        return this;
    }
    
    // ==================== toString ====================
    
    @Override
    public String toString() {
        return "Result{" +
               "code=" + code +
               ", message='" + message + '\'' +
               ", data=" + data +
               ", timestamp=" + timestamp +
               ", traceId='" + traceId + '\'' +
               '}';
    }
}
